/*------------------------------------------------------------------------------
 * @file    SINGER_BUZZ.C
 * @author  嵌入式王子
 * @email   donminedm@163.com
 * @date    2024/07/18 21:51:42
 * @brief   开源的嵌入式软件包，本人不对此代码负责任
 * -----------------------------------------------------------------------------*/

#include "singer_buzz.h"

void singer_buzz_set_note(uint16_t psc)
{
    __HAL_TIM_PRESCALER(&SINGER_BUZZ_TIM, psc); // 通过修改分频率值修改PWM输出频率
}

void singer_buzz_set_vol(float vol)
{
    uint16_t duty = (float)(SINGER_BUZZ_TIM_ARR * vol);
    __HAL_TIM_SetCompare(&SINGER_BUZZ_TIM, SINGER_BUZZ_CHANNEL, duty); // 通过修改重装值修改PWM占空比
}

void singer_buzz_stop(void)
{
    HAL_TIM_PWM_Stop(&SINGER_BUZZ_TIM, SINGER_BUZZ_CHANNEL);
}

void singer_buzz_start(void)
{
    HAL_TIM_PWM_Start(&SINGER_BUZZ_TIM, SINGER_BUZZ_CHANNEL);
}

void singer_buzz_play(uint16_t *music, uint16_t music_len, MUSIC_BEAT beat)
{
    singer_buzz_start();
    for (uint8_t i = 0; i < (music_len / 2); i++)
    {
        singer_buzz_set_note(music[i]); // 播放当前音阶
        SINGER_BUZZ_DELAY(beat);        // 播放音阶时长
    }
    singer_buzz_stop();
}